package demo1;

class MySingleLinkedList{
    class ListNode{
        public int data;
        public ListNode next;
        public ListNode(int data) {
            this.data = data;
        }
        public ListNode() {

        }
    }
    public ListNode head;//指向头结点


    //头插法
    public void addFirst(int data){
        ListNode newNode = new ListNode(data);
        /*if(head == null) {
            head = newNode;
            return;
        }*/
        //不难发现无论链表是否为空，头插并不会被影响
        newNode.next = head;
        head = newNode;
    }
    //尾插法
    public void addLast(int data){
        ListNode newNode = new ListNode(data);
        if(head == null) {
            head = newNode;
            return;
        }
        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = newNode;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        //1.判断index的合法性
        if(index<0 || index>size()) {
            throw new IndexOutOfBoundsException("该位置非法存在！"+index);
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode newNode = new ListNode(data);
        //2.找到插入位置
        ListNode cur = searchIndex(index);
        //3.进行newNode插入
        newNode.next = cur.next;
        cur.next = newNode;
    }
    private ListNode searchIndex(int index) {
        ListNode cur = head;
        while((index-1) > 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(head == null){
            return false;
        }
        ListNode cur = head;
        while(cur != null){
            if(cur.data == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head == null){
            System.out.println("该链表为空链表！");
            return;
        }
        if(head.data == key) {
            head = head.next;
            return;
        }
        ListNode cur = searchPrev(key);
        if(cur == null) {
            System.out.println("不存在！");
            return;
        }
        cur.next = cur.next.next;
    }
    private ListNode searchPrev(int key) {
        ListNode prev = head;
        while(prev.next != null) {
            if(prev.next.data == key) {
                return prev;
            }
            prev = prev.next;
        }
        return null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            System.out.println("该链表为空链表 ！");
        }
        //方案一采取循环直到头结点不为key为止
        /*while(head.data == key) {
            head = head.next;
        }*/
        ListNode cur = head.next;
        ListNode prev = head;
        while(cur != null){
            if(cur.data == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        //方案二：将除头结点外所有结点均删除完成，最后判断头结点是否为key
        if(head.data == key) {
            head = head.next;
        }
    }
    //得到单链表的长度
    public int size(){
        if(head == null){
            return 0;
        }
        ListNode cur = head;
        int length = 0;
        while(cur != null){
            length++;
            cur = cur.next;
        }
        return length;
    }
    public void clear() {
        head = null;
    }
    //打印链表
    public void display() {
        if (head == null) {
            System.out.println("该链表为空链表！");
            return;
        }
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
    }

    //遍历链表
    public void show() {
        if (head == null) {
            System.out.println("该链表为空链表！");
            return;
        }
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
    }
    //指定位置开始打印
    public void show(ListNode head) {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
    }
    public ListNode reverseList() {
        if (head == null) {
            return null;
        }
        if(head.next == null) {
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null) {
            ListNode next = cur.next;
            cur.next = head;
            head = cur;
            cur = next;
        }
        return head;
    }
    //查找中间元素，若是偶数个数元素，中间两个元素选后面一个元素
    public ListNode findMiddle(ListNode head) {
        if(head == null) {
            return null;
        }
        if(head.next == null) {
            return head;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    //找到链表之中倒数第k个元素
    public ListNode findkthToTail(int k) {
        if(k <= 0 || head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        //先让fast先走k-1步
        while(k != 1) {
            fast = fast.next;
            if(fast == null) {
                return null;
            }
            k--;
        }
        //再让slow和fast一起走，直到fast走到tail指针为止
        while(fast.next != null ) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    //合并两个有序链表，合成一个更大的有序链表
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode newhead = new ListNode();
        ListNode cur = newhead;
        while(l1 != null && l2 != null) {
            if(l1.data < l2.data) {
                cur.next = l1;
                cur = cur.next;
                l1 = l1.next;
            } else {
                cur.next = l2;
                cur = cur.next;
                l2 = l2.next;
            }
        }
        if(l1 == null) {
            cur.next = l2;
        }
        if(l2 == null) {
            cur.next = l1;
        }
        return newhead.next;
    }
    //
    public ListNode partition(ListNode head,int k) {
        ListNode smallbegin = null;
        ListNode smallend = null;
        ListNode bigbegin = null;
        ListNode bigend = null;
        ListNode cur = head;
        while(head != null) {
            if(head.data < k) {
                if(smallbegin == null) {
                    smallbegin = cur;
                    smallend = cur;
                    //cur = cur.next;
                } else {
                    smallend.next = cur;
                    //cur = cur.next;
                    smallend = smallend.next;
                }
            } else {
                if(bigbegin == null) {
                    bigbegin = cur;
                    bigend = cur;
                    //cur = cur.next;
                } else {
                    bigend.next = cur;
                    //cur = cur.next;
                    bigend = bigend.next;
                }
            }
            cur = cur.next;
        }
        smallend.next = bigbegin;
        if(smallbegin == null) {
            return bigbegin;
        }
        //若后面链表中最后一个元素并非原链表最后一个元素，此时后面链表的next指针并非空指针，此时将会进入无限循环
        if(bigbegin != null) {
            bigend.next = null;
        }
        return smallbegin;
    }
    //判断是否为回文链表
    public boolean isPalindrome(ListNode head) {
        if(head == null) {
            return false;
        }
        if(head.next == null) {
            return true;
        }
        //1.找到中间结点
        ListNode mid = findMiddle(head);
        MySingleLinkedList list = new MySingleLinkedList();
        list.head = mid;
        //2.翻转链表
        list.reverseList();
        //3.判断是否回文
        while(head != list.head) {
            if(head.data != list.head.data) {
                return false;
            }
            if(head.next == list.head) {
                return true;
            }
            head = head.next;
            list.head = list.head.next;
        }
        return true;
    }
    //找到两个链表相交项
    public ListNode getIntersectionNode(ListNode headA,ListNode headB) {
        ListNode plong = headA;
        ListNode pshort = headB;
        //1.分别求出两个链表的长度
        int lengthA = 0;
        int lengthB = 0;
        while(plong != null) {
            lengthA++;
            plong = plong.next;
        }
        while(pshort != null) {
            lengthB++;
            pshort = pshort.next;
        }
        //2.确保diff为正数，并且plong和pshort分别代表长链表和短链表
        int diff = lengthA - lengthB;
        if(diff < 0) {
            plong = headB;
            pshort = headA;
            diff = lengthB - lengthA;
        }
        //3，哪个链表先走diff步
        while(diff != 0) {
            plong = plong.next;
            diff--;
        }
        /*while(plong != null || pshort != null) {
            if(plong.data == pshort.data) {
                return plong;
            }
            plong = plong.next;
            pshort = pshort.next;
        }*/
        //一起走，直到相遇，并不需要注意两者完全不同的情况，两者最后均会同时走到最后指针的next即null，最终返回值null
        while(plong != pshort ) {
            plong = plong.next;
            pshort = pshort.next;
        }
        return plong;
    }
    //判断链表是否有环
    public boolean hasCycle(ListNode head) {
        if (head == null) {
            return false;
        }
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }
    //寻找环的起点
    public ListNode detectCycle(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        if (fast == null || fast.next == null) {
            return null;
        }
        fast = head;
        while (slow != fast ) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}
public class TestThree {
    public static void main(String[] args) {
        MySingleLinkedList list = new MySingleLinkedList();
        list.show();
        list.addFirst(12);
        list.addFirst(56);
        list.addFirst(34);
        list.addFirst(56);
        list.addFirst(56);
        list.addFirst(78);
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        list.addLast(4);
        list.addIndex(4,99);
        list.addIndex(0,199);
        list.addIndex(10,299);
        /*try {
            list.addIndex(31,8999);
        }catch (IndexOutOfBoundsException e){
            e.printStackTrace();
        }*/
        list.remove(78);
        list.remove(199);
        list.remove(299);
        list.removeAllKey(56);
        list.show();
        int size = list.size();
        System.out.println(size);
        list.reverseList();
        list.show();
        System.out.println();
        MySingleLinkedList.ListNode ret = list.reverseList();
        list.show(ret);
        MySingleLinkedList.ListNode res = list.findkthToTail(6);
        System.out.println(res.data);
    }
}